package com.zl.payment.controller;


import com.zl.payment.entity.HistoryBill;
import com.zl.payment.service.IHistoryBillService;
import com.zl.payment.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄军鹏
 * @since 2020-07-15
 */
@Controller
@RequestMapping("/payment/history-bill")
public class HistoryBillController {
    @Autowired
    private IHistoryBillService historyBillService;
    
    @GetMapping("history/{creditCardId}")
    public String historyBill(@PathVariable("creditCardId") String creditCardId,Model model){
        HistoryBill historyBill = historyBillService.getHistoryBill(creditCardId);
        BigDecimal totalBill = historyBillService.getTotalBill(creditCardId, historyBill.getUpdateTime());
        model.addAttribute("historyBill",historyBill);
        model.addAttribute("totalBill",totalBill);
        return "view/history";
    }
    
    @PostMapping("repayHistory/{creditCardId}")
    public String repayHistoryBill(@PathVariable("creditCardId") String creditCardId, BigDecimal repayAmount,String cardNo, Model model){
        boolean repayBill = historyBillService.repayBill(creditCardId, DateUtils.today(), repayAmount, cardNo);
        if (repayBill){
            model.addAttribute("message","历史账单还款成功");
            return "view/repayResult";
        }
        model.addAttribute("message","历史账单还款失败");
        return "view/repayResult";
    }
}
